package com.zhonglz.system.controller;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhonglz.commons.base.BaseController;
import com.zhonglz.model.SysFileInfo;
import com.zhonglz.util.minio.MinioUtils;
import com.zhonglz.util.result.ResultUtils;
import com.zhonglz.util.tools.DataGrid;
import com.zhonglz.util.tools.ObjectUtils;

import java.io.OutputStream;

/****
 * 文件接口
 * @author zhonglz
 */
@RestController
@RequestMapping("/sys/file")
public class FileController extends BaseController{
	
	/***
	 * 分页查询列表
	 * @param sysFileInfo
	 * @param startTime
	 * @param endTime
	 * @param pageNumber
	 * @param offset
	 * @param limit
	 * @param req
	 * @return
	 */
	@ResponseBody
    @RequestMapping("selectListByData")
    public DataGrid selectListByData(SysFileInfo sysFileInfo,
    		@RequestParam(defaultValue="") String startTime,
    		@RequestParam(defaultValue="") String endTime,
    		@RequestParam(defaultValue = "1") int pageNumber,
			@RequestParam(defaultValue = "10") int pageSize,
			HttpServletRequest req) {
    	DataGrid dataGrid = null;
		// 封装查询条件参数
		Map<String, Object> mapParam = new HashMap<String, Object>();
		if(ObjectUtils.isNotEmpty(startTime)){
			startTime+=" 00:00:00";
			mapParam.put("startTime",startTime);
		}
		if(ObjectUtils.isNotEmpty(endTime)){
			endTime+=" 23:59:59";
			mapParam.put("endTime",endTime);
		}
		if(ObjectUtils.isNotEmpty(sysFileInfo)){
			if(ObjectUtils.isNotEmpty(sysFileInfo.getFileUid())){
				mapParam.put("fileUid",sysFileInfo.getFileUid());
			}
			if(ObjectUtils.isNotEmpty(sysFileInfo.getFileRealName())){
				mapParam.put("fileRealName",sysFileInfo.getFileRealName());
			}
			if(ObjectUtils.isNotEmpty(sysFileInfo.getFileAddress())){
				mapParam.put("fileAddress",sysFileInfo.getFileAddress());
			}
		}
		PageHelper.startPage(pageNumber, pageSize);
		List<SysFileInfo> selectFileInfoList=sysFileInfoService.selectByDataList(mapParam);
		PageInfo<SysFileInfo> info = new PageInfo<SysFileInfo>(selectFileInfoList);
		// 封装结果集
		dataGrid = new DataGrid(info.getTotal(),selectFileInfoList,info.getPages(),info.getPageNum(),ResultUtils.CODE_SUCCESS);
    	return dataGrid; 
	}
	
	/****
	 * 文件上传
	 * @param file
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/uploadFile", method =RequestMethod.POST )
	public ResultUtils uploadFile(MultipartFile file){
		ResultUtils result =sysFileInfoService.uploadFile(file);
		return result;
	}
	
	
	
	
	
	/****
	 * 文件上传 文件base64
	 * @param imgBase64Str
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/uploadFileToBase", method =RequestMethod.POST )
	public ResultUtils uploadFileToBase(String imgBase64Str){
		ResultUtils result =sysFileInfoService.uploadFileToBase(imgBase64Str);
		return result;
	}
	
	
	 /**
	 * 下载文件(流形式，一般用作标签的src)
	 * @param request
	 * @param response
	 * @param fileUid 文件UUid
	 */
	 @RequestMapping(value = "/downloadFileByUUid", method = RequestMethod.GET)
	 public void downloadFileByUUid(HttpServletRequest request, HttpServletResponse response,String fileUid) {
		ResultUtils result =sysFileInfoService.selectFileByUid(fileUid);
		if(result.getCode()==ResultUtils.CODE_SUCCESS) {
			SysFileInfo info =(SysFileInfo) result.getObject();
			Integer flag;
			try {
				OutputStream out = response.getOutputStream();
				InputStream input = MinioUtils.getObject(info.getFileAddress());
				request.setCharacterEncoding("UTF-8");
				response.setContentType("image/png");
				IOUtils.copy(input, out);
				out.flush();
				out.close();
				flag = 1;
			} catch (Exception e) {
				e.printStackTrace();
				flag = 0;
			}
			request.setAttribute("flag", flag);
		}
    }
	 /**
		 * 下载文件(流形式，一般用作标签的src)
		 * @param request
		 * @param response
		 * @param fileUid 文件UUid
		 */
		 @RequestMapping(value = "/downloadPdfByUUid", method = RequestMethod.GET)
		 public void downloadPdfByUUid(HttpServletRequest request, HttpServletResponse response,String fileUid) {
			ResultUtils result =sysFileInfoService.selectFileByUid(fileUid);
			if(result.getCode()==ResultUtils.CODE_SUCCESS) {
				SysFileInfo info =(SysFileInfo) result.getObject();
				Integer flag;
				try {
					OutputStream out = response.getOutputStream();
					InputStream input = MinioUtils.getObject(info.getFileAddress());
					request.setCharacterEncoding("UTF-8");
					response.setContentType("application/x-msdownload");
					response.setHeader("Content-Disposition", "attachment; filename=" +info.getFileStoreName());
					IOUtils.copy(input, out);
					out.flush();
					out.close();
					flag = 1;
				} catch (Exception e) {
					e.printStackTrace();
					flag = 0;
				}
				request.setAttribute("flag", flag);
			}
	    	
	    }
	
	/**
	 * 文件转换接口
	 */
	 @RequestMapping(value = "/fileToPdf", method = RequestMethod.POST)
	 public ResultUtils fileToPdf(String fileUid,Byte type) {
		if(ObjectUtils.isEmpty(fileUid))  return new ResultUtils(ResultUtils.CODE_ERROR_PARAM,"文件编号为空！");
		ResultUtils result =sysFileInfoService.selectFileByUid(fileUid);
		if(result.getCode() != ResultUtils.CODE_SUCCESS) return new ResultUtils(ResultUtils.CODE_ERROR_PARAM,"当前源文件已被删除或不存在！请重新上传");
    	return sysFileInfoService.fileConvertByType((SysFileInfo)result.getObject(),type);
    }
	 

}
